package space.yangmin.baidu.downloader.service.impl;

import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.AllArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.util.UriComponentsBuilder;
import space.yangmin.baidu.downloader.bo.FileInfoBO;
import space.yangmin.baidu.downloader.dto.FileInfoDTO;
import space.yangmin.baidu.downloader.dto.ListFileDTO;
import space.yangmin.baidu.downloader.service.FileService;
import space.yangmin.baidu.downloader.util.DownloadUtil;

import java.io.File;
import java.util.List;

/**
 * @Description TODO
 * @Version 1.0.0
 * @Date 2024/8/7/0007 10:47
 * @Author Administrator
 */
@Service
@AllArgsConstructor
public class FileServiceImpl implements FileService {
    @Override
    public List<FileInfoBO> listFile(ListFileDTO dto) {
        /*url构造*/
        dto.setDir(URLUtil.encodeAll(dto.getDir()));
        UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl("https://pan.baidu.com/rest/2.0/xpan/file");
        uriBuilder.queryParam("method","list");
        uriBuilder.queryParam("access_token",this.getAccessToken());
        this.buildUri(uriBuilder, dto);

        /*请求、解析*/
        String url = uriBuilder.build().toUriString();
        HttpResponse httpResponse = HttpRequest.get(url)
                .header("User-Agent", "pan.baidu.com")
                .execute();
        Assert.isTrue(httpResponse.isOk(), "文件列表接口异常：\n" + httpResponse);
        String body = httpResponse.body();
        Assert.isTrue(JSON.isValid(body), "文件列表接口请求失败：\n" + httpResponse);
        JSONObject json = JSON.parseObject(body);
        JSONArray jsonArray = json.getJSONArray("list");
        return JSON.parseArray(jsonArray.toJSONString(), FileInfoBO.class);
    }

    @Override
    public List<FileInfoBO> getFileInfo(FileInfoDTO dto) {
        /*url构造*/
        UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl("https://pan.baidu.com/rest/2.0/xpan/multimedia");
        uriBuilder.queryParam("method","filemetas");
        uriBuilder.queryParam("access_token",this.getAccessToken());
        this.buildUri(uriBuilder, dto);

        /*请求、解析*/
        String url = uriBuilder.build().toUriString();
        HttpResponse httpResponse = HttpRequest.get(url).execute();
        Assert.isTrue(httpResponse.isOk(), "文件信息接口异常：\n" + httpResponse);
        String body = httpResponse.body();
        Assert.isTrue(JSON.isValid(body), "文件信息接口请求失败：\n" + httpResponse);
        JSONObject jsonObject = JSON.parseObject(body);
        Assert.isTrue(jsonObject.containsKey("list"), "文件信息接口请求失败：\n" + body);
        return JSON.parseArray(jsonObject.getJSONArray("list").toJSONString(), FileInfoBO.class);
    }

    @Async
    @Override
    public String downloadFile(FileInfoBO fileInfoBO, String directory) {
        File file = DownloadUtil.download(fileInfoBO.getDlink() + "&access_token=" + this.getAccessToken(), fileInfoBO.getFilename(), directory);
        return file.getAbsolutePath();
    }
}
